package edu.ynu.se.xiecheng.achitectureclass.entity;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.LogicEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

@Entity
@Getter
@Setter
@Where(clause = "is_deleted = 0")
public class Item extends LogicEntity {
    @Column
    private String itemName;

    @OneToMany(mappedBy = "item",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
//    @JsonIgnore
    @JsonIgnoreProperties("item")
    private Set<LineItem> lineItems;

    @OneToMany(mappedBy = "item",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("item")
    private Set<ShopItem> shopItems;

    public ShopItem findShopItem(Shop shop){
        AtomicReference<ShopItem> result = new AtomicReference<>(null);
        shopItems.forEach(shopItem -> {
            if(shopItem.getShop().equals(shop))
                result.set(shopItem);
        });
        return result.get();
    }
}
